# 余弦相似度分布
np.random.seed(42)
fedavg_sim = np.random.uniform(0.4, 0.7, 100)    # FedAvg
dhaf_sim = np.random.uniform(0.7, 0.9, 100)      # DHA-FL

# 绘制散点分布图
plt.figure(figsize=(5,3))
plt.scatter(range(100), fedavg_sim, s=50, alpha=0.6, 
            label=f'FedAvg (Mean={np.mean(fedavg_sim):.2f})', color='#FFBE7A',marker='^')
plt.scatter(range(100), dhaf_sim, s=50, alpha=0.6, 
            label=f'DHA-FL (Mean={np.mean(dhaf_sim):.2f})', color='#82B0D2')

# 标注提升幅度
# plt.annotate('↑41.2%', xy=(50, 0.8), xytext=(50, 0.85),
#              arrowprops=dict(arrowstyle='->', color='black'), ha='center')

plt.axhline(y=np.mean(fedavg_sim), color='#FFBE7A', linestyle='-.', alpha=0.7)
plt.axhline(y=np.mean(dhaf_sim), color='#82B0D2', linestyle='--', alpha=0.7)

plt.xlabel('设备索引', fontsize=12)
plt.ylabel('余弦相似度', fontsize=12)
# plt.title('Gradient Conflict Mitigation Effect', fontsize=14)
plt.legend(loc='lower right')
plt.grid(alpha=0.3)
plt.savefig('gradient_similarity.png', bbox_inches='tight')
